﻿#include "gameworld.h"
#pragma execution_character_set("utf-8")
GameWorld* GameWorld::_obj = nullptr;

GameWorld::GameWorld()
{

    myWorld = new btosgWorld;
    //设置重力方向
    btosgVec3 up(0., 0., 1.);
    //模拟真实的重力场景
    btosgVec3 gravity = up*-9.8;
    //设置重力方向
    myWorld->dynamic->setGravity(gravity);
    //设置车子的方位
    myVehicle = new btosgVehicle(myWorld);
    //重点 这里除了乘以3之后，还在后面又乘以了10
    myVehicle->setPosition(btosgVec3(up*3.));
    //名字
    myVehicle->setName("Vehicle");
    //质量
    myVehicle->setMass(800.);
    //世界中添加刚体
    myWorld->addObject( myVehicle );
    //
    myVehicle->printInfo();

    //
    osgUtil::SmoothingVisitor* sv = new osgUtil::SmoothingVisitor;
    //重点
    myVehicle->pNode->accept(*sv);


    osg::StateSet* stateSet2 = myWorld->scene->getOrCreateStateSet();
    stateSet2->setMode( GL_DEPTH_TEST, osg::StateAttribute::ON );

    //这段代码好像没有用到
//    osg::ref_ptr<osg::Material> matCylinder = new osg::Material;
//    matCylinder->setAmbient (osg::Material::FRONT_AND_BACK, osg::Vec4(0.0, 0.,  0.,  1.0));
//    matCylinder->setDiffuse (osg::Material::FRONT_AND_BACK, osg::Vec4(0.6, 0.4, 0.1, 1.0));
//    matCylinder->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.,  0.,  0.,  1.0));
//    matCylinder->setShininess(osg::Material::FRONT_AND_BACK, 64);


    btosgPlane *myRamp = new btosgPlane(osg::Vec3(50.,50.,50.) - up*50.);
    myRamp->setPosition(0.,0.,0.);
    myWorld->addObject( myRamp );
    myRamp->setName("Ramp");
//    myRamp->body->setFriction(100.);
    myRamp->body->setFriction(1000.);
    osg::ref_ptr<osg::Material> matRamp = new osg::Material;
    matRamp->setAmbient (osg::Material::FRONT_AND_BACK, osg::Vec4(0., 0., 0., 1.0));
    matRamp->setDiffuse (osg::Material::FRONT_AND_BACK, osg::Vec4(0.7, 0.8, 0.0, 1.0));
    matRamp->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1.0));
    matRamp->setShininess(osg::Material::FRONT_AND_BACK, 64);
    myRamp->setMaterial(matRamp);

    osg::ref_ptr<osg::LightSource> ls = new osg::LightSource;
    ls->getLight()->setPosition(osg::Vec4(2.5,-10+30*up[1],-10+30.*up[2],1.));
    ls->getLight()->setAmbient(osg::Vec4(0.1, 0.1, 0.1, 1.0));
    ls->getLight()->setDiffuse(osg::Vec4(1.0, 1.0, 1.0, 1.0));
    ls->getLight()->setSpecular(osg::Vec4(0.2, 0.2, 0.2, 1.0));
    myWorld->scene->addChild(ls.get());

    // Set the desired home coordinates for the manipulator
    osg::Vec3d eye(osg::Vec3(0., -5., -5.)+up*20.);
    osg::Vec3d center(0., 0., 0.);

    manipulator = new osgGA::TrackballManipulator;

    // Make sure that OSG is not overriding our home position
    manipulator->setAutoComputeHomePosition(false);
    // Set the desired home position of the Trackball Manipulator
    manipulator->setHomePosition(eye, center, up);
    // Force the camera to move to the home position
    manipulator->home(0.0);



}

GameWorld::~GameWorld()
{
    delete myWorld;
    delete myVehicle;
//    delete timer;
    delete viewer;

}

GameWorld *GameWorld::getHandle()
{
    if(!_obj)
        _obj = new GameWorld;
    return _obj;

}

void GameWorld::setViewer(osgViewer::Viewer* _viewer)
{
    viewer = _viewer;

}

void GameWorld::update()
{

}
